package com.example.myweb.Controller.Auth;

import com.example.myweb.Common.ApiResponse;
import com.example.myweb.Domain.Entity.Comment;
import com.example.myweb.Domain.Entity.User;
import com.example.myweb.Dto.CommentDTO;
import com.example.myweb.Dto.CreateCommentDTO;
import com.example.myweb.Service.Auth.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
public class ContentInteractionController {

    @Autowired
    private CommentService commentService;
    private static final String ARTICLE = "articles";
    private static final String DYNAMIC = "dynamics";

    // --- 评论动态 ---

//    @PostMapping("/dynamics/{contentId}/comments")
//    public ResponseEntity<ApiResponse<Map<String, Object>>> createDynamicComment(
//            @PathVariable Long contentId,
//            @Valid @RequestBody CreateCommentDTO request,
//            @AuthenticationPrincipal User currentUser) {
//
//        Comment comment = commentService.createComment(Comment.ContentType.DYNAMIC, contentId, request, currentUser);
//        Map<String, Object> responseData = Map.of("commentId", comment.getCommentId());
//
//        return new ResponseEntity<>(ApiResponse.success(responseData, "评论发布成功"), HttpStatus.CREATED);
//    }
//
//    @GetMapping("/dynamics/{contentId}/comments")
//    public ResponseEntity<ApiResponse<Page<CommentDTO>>> getDynamicComments(
//            @PathVariable Long contentId,
//            @RequestParam(defaultValue = "0") int page,
//            @RequestParam(defaultValue = "10") int size) {
//
//        Pageable pageable = PageRequest.of(page, size, Sort.by("commentTime").ascending()); // 评论通常按时间正序
//        Page<CommentDTO> comments = commentService.getComments(Comment.ContentType.DYNAMIC, contentId, pageable);
//
//        return ResponseEntity.ok(ApiResponse.success(comments, "评论获取成功"));
//    }
//
//    // --- 评论文章 (逻辑完全相同，只是路径和类型不同) ---
//
//    @PostMapping("/articles/{contentId}/comments")
//    public ResponseEntity<ApiResponse<Map<String, Object>>> createArticleComment(
//            @PathVariable Long contentId,
//            @Valid @RequestBody CreateCommentDTO request,
//            @AuthenticationPrincipal User currentUser) {
//
//        Comment comment = commentService.createComment(Comment.ContentType.ARTICLE, contentId, request, currentUser);
//        Map<String, Object> responseData = Map.of("commentId", comment.getCommentId());
//
//        return new ResponseEntity<>(ApiResponse.success(responseData, "评论发布成功"), HttpStatus.CREATED);
//    }
//
//    @GetMapping("/articles/{contentId}/comments")
//    public ResponseEntity<ApiResponse<Page<CommentDTO>>> getArticleComments(
//            @PathVariable Long contentId,
//            @RequestParam(defaultValue = "0") int page,
//            @RequestParam(defaultValue = "10") int size) {
//
//        Pageable pageable = PageRequest.of(page, size, Sort.by("commentTime").ascending());
//        Page<CommentDTO> comments = commentService.getComments(Comment.ContentType.ARTICLE, contentId, pageable);
//
//        return ResponseEntity.ok(ApiResponse.success(comments, "评论获取成功"));
//    }

    /**
            * 对文章或动态发表评论
     */
    @PostMapping("/{contentType:articles|dynamics}/{contentId}/comments")
    public ResponseEntity<ApiResponse<Map<String, Object>>> createComment(
            @PathVariable String contentType,
            @PathVariable Long contentId,
            @Valid @RequestBody CreateCommentDTO request,
            @AuthenticationPrincipal User currentUser) {

        Comment.ContentType type = Comment.ContentType.valueOf(contentType.substring(0, contentType.length() - 1).toUpperCase());
        Comment comment = commentService.createComment(type, contentId, request, currentUser);
        return new ResponseEntity<>(ApiResponse.success(Map.of("commentId", comment.getCommentId()), "评论成功"), HttpStatus.CREATED);
    }

    /**
     * 获取文章或动态的评论列表
     */
    @GetMapping("/{contentType:articles|dynamics}/{contentId}/comments")
    public ResponseEntity<ApiResponse<Page<CommentDTO>>> getComments(
            @PathVariable String contentType,
            @PathVariable Long contentId,
            Pageable pageable,
            @AuthenticationPrincipal User currentUser) { // currentUser 可以为 null

        Comment.ContentType type = Comment.ContentType.valueOf(contentType.substring(0, contentType.length() - 1).toUpperCase());
        Page<CommentDTO> comments = commentService.getComments(type, contentId, pageable, currentUser);
        return ResponseEntity.ok(ApiResponse.success(comments, "获取评论成功"));
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/comments/{commentId}")
    public ResponseEntity<ApiResponse<Void>> deleteComment(
            @PathVariable Long commentId,
            @AuthenticationPrincipal User currentUser) {
        commentService.deleteComment(commentId, currentUser);
        return ResponseEntity.ok(ApiResponse.success("删除评论成功"));
    }

}